1
|
|
|
const fs = require("fs"); |
2
|
|
|
const xml2js = require("xml2js"); |
3
|
|
|
const xmlParser = new xml2js.Parser(); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Reads a JSON file and returns the parsed JSON data |
7
|
|
|
* |
8
|
|
|
* @param {string} jsonFile Path to the json file |
9
|
|
|
* |
10
|
|
|
* @returns {string} Empty json object if file does not exist |
11
|
|
|
*/ |
12
|
|
|
function readJSON(jsonFile) { |
13
|
|
|
if (!fs.existsSync(jsonFile)) { |
14
|
|
|
return "{}"; |
15
|
|
|
} |
16
|
|
|
return JSON.parse(fs.readFileSync(jsonFile).toString()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Clones an object |
21
|
|
|
* |
22
|
|
|
* @param object |
23
|
|
|
* |
24
|
|
|
* @returns object |
25
|
|
|
*/ |
26
|
|
|
function cloneObject(object) { |
27
|
|
|
return JSON.parse(JSON.stringify(object)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get a list of files over a parent directory |
32
|
|
|
* |
33
|
|
|
* @param {string} dir Parent directory |
34
|
|
|
* @param {string} type Type of files: "directories" or "files" |
35
|
|
|
* |
36
|
|
|
* @returns {Array} |
37
|
|
|
*/ |
38
|
|
|
function getFilesbyType(dir, type) { |
39
|
|
|
const files = fs.readdirSync(dir); |
40
|
|
|
const dirs = []; |
41
|
|
|
files.forEach(function (file) { |
42
|
|
|
const isDirectory = fs.statSync(dir + "/" + file).isDirectory(); |
43
|
|
|
if ( |
44
|
|
|
(type === "directory" && isDirectory) || |
45
|
|
|
(type !== "directory" && !isDirectory) |
46
|
|
|
) { |
47
|
|
|
dirs.push(file); |
48
|
|
|
} |
49
|
|
|
}); |
50
|
|
|
return dirs; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the version of a Joomla xml manifest file |
55
|
|
|
* |
56
|
|
|
* @param {string} xmlFile Input XML manifest file (Joomla format) |
57
|
|
|
* |
58
|
|
|
* @returns {string} |
59
|
|
|
*/ |
60
|
|
|
function getManifestVersion(xmlFile) { |
61
|
|
|
let version = ""; |
62
|
|
|
xmlParser.parseString(fs.readFileSync(xmlFile), function (err, data) { |
63
|
|
|
if (err) { |
64
|
|
|
return ""; |
65
|
|
|
} |
66
|
|
|
version = data.extension.version; |
67
|
|
|
return version; |
68
|
|
|
}); |
69
|
|
|
return version; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
exports.readJSON = readJSON; |
73
|
|
|
exports.cloneObject = cloneObject; |
74
|
|
|
exports.getFilesByType = getFilesbyType; |
75
|
|
|
exports.getManifestVersion = getManifestVersion; |
76
|
|
|
|